package net.qhzw.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.qhzw.admin.entity.PersonnelFacinfo;
import net.qhzw.admin.mapper.PersonnelFacinfoMapper;
import net.qhzw.admin.param.PersonnelFacinfoParam;
import net.qhzw.admin.pojo.PersonnelFacinfoVo;
import net.qhzw.admin.pojo.PersonnelFacinfoVo2;
import net.qhzw.admin.query.PersonnelFacinfoQuery;
import net.qhzw.admin.service.PersonnelFacinfoService;
import net.qhzw.common.model.JsonResult;
import net.qhzw.common.util.StringUtils;
import net.qhzw.common.util.UUIDUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PersonnelFacinfoServiceImpl implements PersonnelFacinfoService {

    @Autowired
    private PersonnelFacinfoMapper personnelFacinfoMapper;

    @Value("${config.imgage.view}")
    private String imageView;

    @Override
    public JsonResult getPersonnelFacinfo(String id) {
        PersonnelFacinfoVo2 personnelFacinfo = personnelFacinfoMapper.selectPersonnelFacinfo(id);
        Date[] date = new Date[2];
        if (personnelFacinfo.getStartTime() != null) {
            date[0] = personnelFacinfo.getStartTime();
        }

        if (personnelFacinfo.getEndTime() != null) {
            date[1] = personnelFacinfo.getEndTime();
        }
        personnelFacinfo.setTeachingTime(date);
        return JsonResult.success(personnelFacinfo);
    }

    @Override
    public JsonResult getPersonnelFacinfoList(PersonnelFacinfoQuery query) {
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(query.getPage(), query.getLimit());
        if (query.getTeachingTime() != null && query.getTeachingTime().length > 0) {
            query.setStartTime(query.getTeachingTime()[0]);
            query.setEndTime(query.getTeachingTime()[1]);
        }
        List<PersonnelFacinfoVo> personnelFacinfoVoList = personnelFacinfoMapper.selectList(query);
        for (PersonnelFacinfoVo personnelFacinfoVo : personnelFacinfoVoList) {
            if (personnelFacinfoVo.getPhoto() != null && !StringUtils.isEmpty(personnelFacinfoVo.getPhoto())) {
                personnelFacinfoVo.setPhoto(imageView + personnelFacinfoVo.getPhoto());
            }
        }
        PageInfo<PersonnelFacinfoVo> pageInfo = new PageInfo<>(personnelFacinfoVoList);
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("list", pageInfo.getList());
        return JsonResult.success(resultMap);
    }


    /***
     * 更新教职信息
     * @param param
     * @return
     */
    @Override
    public JsonResult updatePersonnelFacinfo(PersonnelFacinfoParam param) {
        if (param.getId() == null || StringUtils.isEmpty(param.getId())) {
            //添加
            PersonnelFacinfo personnelFacinfo = new PersonnelFacinfo();
            BeanUtils.copyProperties(param, personnelFacinfo);
            if (param.getTeachingTime() != null && param.getTeachingTime().length > 0) {
                personnelFacinfo.setStartTime(param.getTeachingTime()[0]);
                personnelFacinfo.setEndTime(param.getTeachingTime()[1]);
            }
            personnelFacinfo.setCreateTime(new Date());
            personnelFacinfo.setId(UUIDUtil.getUUid());
            int row = personnelFacinfoMapper.insertSelective(personnelFacinfo);
            return JsonResult.result(row, "新增失败");
        }
        //更新
        PersonnelFacinfo personnelFacinfo = new PersonnelFacinfo();
        BeanUtils.copyProperties(param, personnelFacinfo);
        if (param.getTeachingTime() != null && param.getTeachingTime().length > 0) {
            personnelFacinfo.setStartTime(param.getTeachingTime()[0]);
            personnelFacinfo.setEndTime(param.getTeachingTime()[1]);
        }
        int row = personnelFacinfoMapper.updateByPrimaryKeySelective(personnelFacinfo);
        return JsonResult.result(row, "更新失败");
    }


    /***
     * 根据id删除教职信息
     * @param id
     * @return
     */
    @Override
    public JsonResult deletePersonnelFacinfo(String id) {
        int row = personnelFacinfoMapper.deleteByPrimaryKey(id);
        return JsonResult.result(row, "删除失败");
    }


}
